﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using DPW.Business.Helper;
using DPW.Business.Repository;
using DPW.Models;

namespace DPW.Controllers
{
    public class CategoryController : Controller
    {
        //
        // GET: /Category/

        public ActionResult Index(string title)
        {
            string s = title;
            return View();
        }

        //
        // GET: /Category/Details/5

        public ActionResult Details(int id)
        {
            var model = new CategoryDetailViewModel();
            model.Category = CategoryRepository.GetCategory(id);
            model.Categories = CategoryRepository.GetAllCategories();
            model.LatestPoems = PoemRepository.GetLatestPoems(10);
            model.MostViewedPoems = PoemRepository.GetMostViewedPoems(5);
            model.Poems = CategoryRepository.GetPoemsOfCategory(id);
            return View(model);
        }

        //
        // GET: /Category/Create

        public ActionResult New()
        {
            return View("New");
        }

        //
        // POST: /Category/Create

        [HttpPost]
        public ActionResult New(FormCollection collection)
        {
            try
            {
                string categoryName = collection["CategoryName"];
                string categoryDescription = collection["CategoryDescription"];

                if(!string.IsNullOrEmpty(categoryName))
                {
                    var newCat = CategoryRepository.AddNewCategory(categoryName, categoryDescription);

                    if(newCat != null)
                    {
                        return RedirectToAction("Details", "Category", categoryName);
                    }

                }
                return View();
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Category/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Category/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Category/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Category/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
